--[[
-- added by wsh @ 2017-12-03
-- UI工具类
--]]
local UIUtil = {}

function UIUtil.GetChild(trans, index)
    return trans:GetChild(index)
end

function UIUtil.Find(name)
    return CS.UnityEngine.GameObject.Find(name)
end

function UIUtil.getMainCamera()
    return CS.UnityEngine.Camera.main
end

-- 注意：根节点不能是隐藏状态，否则路径将找不到
function UIUtil.FindComponent(trans, ctype, path)
    assert(trans ~= nil)
    assert(ctype ~= nil)

    local targetTrans = trans
    if path ~= nil and type(path) == "string" and #path > 0 then
        targetTrans = trans:Find(path)
    end
    if targetTrans == nil then
        return nil
    end
    local cmp = targetTrans:GetComponent(ctype)
    if cmp ~= nil then
        return cmp
    end
    return targetTrans:GetComponentInChildren(ctype)
end

function UIUtil.FindTrans(trans, path)
    return trans:Find(path)
end

function UIUtil.getUIComponent(gamobject, name)
    if name == "text" then
        return gamobject:GetComponent(typeof(CS.UnityEngine.UI.Text))
    elseif name == "image" then
        return gamobject:GetComponent(typeof(CS.UnityEngine.UI.Image))
    elseif name == "button" then
        return gamobject:GetComponent(typeof(CS.UnityEngine.UI.Button))
    elseif name == "toggle" then
        return gamobject:GetComponent(typeof(CS.UnityEngine.UI.Toggle))
    end
end

function UIUtil.FindText(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.Text), path)
end

function UIUtil.FindImage(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.Image), path)
end

function UIUtil.FindButton(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.Button), path)
end

function UIUtil.FindInput(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.InputField), path)
end

function UIUtil.FindSlider(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.Slider), path)
end

function UIUtil.FindScrollRect(trans, path)
    return UIUtil.FindComponent(trans, typeof(CS.UnityEngine.UI.ScrollRect), path)
end

function UIUtil.addButtonClick(trans, func)
    local btn = UIUtil.FindButton(trans)
    btn.onClick:AddListener(func)
end

function UIUtil.addTouchEvent(trans, touchBegin, touchMove, touchEnd, touchCancel)
    local trigger = trans.gameObject:GetComponent(typeof(CS.UnityEngine.EventSystems.EventTrigger))
    if (not trigger) then
        trigger = trans.gameObject:AddComponent(typeof(CS.UnityEngine.EventSystems.EventTrigger))
    end
    local entryBeginDrag = CS.UnityEngine.EventSystems.EventTrigger.Entry()
    entryBeginDrag.eventID = CS.UnityEngine.EventSystems.EventTriggerType.PointerDown
    entryBeginDrag.callback:AddListener(
        function(event)
            if touchBegin then
                touchBegin(event)
            end
        end
    )

    local entryDrag = CS.UnityEngine.EventSystems.EventTrigger.Entry()
    entryDrag.eventID = CS.UnityEngine.EventSystems.EventTriggerType.Drag
    entryDrag.callback:AddListener(
        function(event)
            if touchMove then
                touchMove(event)
            end
        end
    )

    local entryEndDrag = CS.UnityEngine.EventSystems.EventTrigger.Entry()
    entryEndDrag.eventID = CS.UnityEngine.EventSystems.EventTriggerType.PointerUp
    entryEndDrag.callback:AddListener(
        function(event)
            if touchEnd then
                touchEnd(event)
            end
        end
    )
    local entryCancelDrag = CS.UnityEngine.EventSystems.EventTrigger.Entry()
    entryCancelDrag.eventID = CS.UnityEngine.EventSystems.EventTriggerType.Cancel
    entryCancelDrag.callback:AddListener(
        function(event)
            if touchCancel then
                touchCancel(event)
            end
        end
    )
    trigger.triggers:Add(entryBeginDrag)
    trigger.triggers:Add(entryDrag)
    trigger.triggers:Add(entryEndDrag)
    trigger.triggers:Add(entryCancelDrag)
end

function UIUtil.createText(parent, position)
    local Vector3 = require("utils.Vector3")
    local Vector2 = require("utils.Vector2")
    local GameUtils = require("utils.GameUtils")
    local textObj = GameUtils.InstantiateEmptyObject("TextObj")
    local rect = textObj:AddComponent(typeof(CS.UnityEngine.RectTransform))
    rect:SetParent(parent)
    rect.sizeDelta = Vector2.new(300, 26)
    rect.pivot = Vector2.new(0, 0.5)
    rect.anchorMax = Vector2.new(0, 0.5)
    rect.anchorMin = Vector2.new(0, 0.5)
    rect.localScale = Vector3.new(1, 1, 1)
    rect.localPosition = position
    local text = textObj:AddComponent(typeof(CS.UnityEngine.UI.Text))
    text.font = G_ResourceManager:loadPrefabs("Prefabs/fonts/font1")
    return text
end

return UIUtil
